// 初始化图表
let chartThree = echarts.init(document.getElementById('main_three'));

// 加载动画
chartThree.showLoading();

// 渲染热销商品TOP10图表
function renderChartThree(topProducts) {
    const products = topProducts.map(item => item.product_name);
    const quantities = topProducts.map(item => item.total_quantity);
    const amounts = topProducts.map(item => item.total_amount);

    const option = {
        title: {
            text: '热销商品TOP10',
            left: 'center'
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            },
            formatter: function(params) {
                const quantity = params[0].value;
                const amount = amounts[params[0].dataIndex];
                return `${params[0].name}<br/>
                        销售数量：${quantity}件<br/>
                        销售金额：￥${amount.toFixed(2)}`;
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: {
            type: 'value',
            name: '销售数量（件）'
        },
        yAxis: {
            type: 'category',
            data: products,
            axisLabel: {
                interval: 0,
                width: 100,
                overflow: 'break'
            }
        },
        series: [{
            name: '销售数量',
            type: 'bar',
            data: quantities,
            itemStyle: {
                color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: '#83bff6'
                }, {
                    offset: 0.5,
                    color: '#188df0'
                }, {
                    offset: 1,
                    color: '#188df0'
                }])
            },
            emphasis: {
                itemStyle: {
                    color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                        offset: 0,
                        color: '#2378f7'
                    }, {
                        offset: 0.7,
                        color: '#2378f7'
                    }, {
                        offset: 1,
                        color: '#83bff6'
                    }])
                }
            }
        }]
    };

    chartThree.hideLoading();
    chartThree.setOption(option);
}

// 监听窗口大小变化
window.addEventListener('resize', function() {
    chartThree.resize();
});
